
# 首先安装redis
mkdir -p /home/elon/redis/{conf,data,log,bin}
mkdir -p /home/elon/redis/data/{6380,6381}
touch /home/elon/redis/conf/{redis-6381.conf,redis-6380.conf,sentinel-26379.conf,sentinel-26380.conf,sentinel-26381.conf}

# 主节点配置 `redis-6380.conf`
port 6380
dir /home/elon/redis/data/6380
logfile /home/elon/redis/logs/redis-6380.log
daemonize yes
pidfile /home/elon/redis/redis-6380.pid

# 从节点配置 `redis-6381.conf`
port 6381
dir /home/elon/redis/data/6381
logfile /home/elon/redis/logs/redis-6381.log
daemonize yes
pidfile /home/elon/redis/redis-6381.pid
replicaof 127.0.0.1 6380

# 修改sentinel-26379.conf, 以此类推sentinel-26380.conf,sentinel-26381.conf
port 26379
daemonize yes
logfile "/home/elon/redis/logs/sentinel-26379.log"
dir "/home/elon/redis/"
sentinel monitor mymaster 127.0.0.1 6380 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 10000
sentinel parallel-syncs mymaster 1

# 记得bin里面要有redis-sentinel
# 启动主节点
/home/elon/redis/bin/redis-server /home/elon/redis/conf/redis-6380.conf

# 启动从节点
/home/elon/redis/bin/redis-server /home/elon/redis/conf/redis-6381.conf

# 启动 Sentinel（3个）
/home/elon/redis/bin/redis-sentinel /home/elon/redis/conf/sentinel-26379.conf
/home/elon/redis/bin/redis-sentinel /home/elon/redis/conf/sentinel-26380.conf
/home/elon/redis/bin/redis-sentinel /home/elon/redis/conf/sentinel-26381.conf

# 查看主从状态
/home/elon/redis/bin/redis-cli -p 6380 info replication
/home/elon/redis/bin/redis-cli -p 6381 info replication

# 测试阶段
/home/elon/redis/bin/redis-cli -p 6380 #连接主库
127.0.0.1:6380> set test_key "hello_from_master"

/home/elon/redis/bin/redis-cli -p 6381 #连接从库

验证只读
127.0.0.1:6381> set slave_key "should_fail"
(error) READONLY You can't write against a read only replica.